package ru.nosport.matrixaria.modules.core.blog;

import ru.nosport.matrixaria.modules.common.AppServerProcess;
import ru.nosport.matrixaria.core.MLogger;
import ru.nosport.matrixaria.core.CoreException;
import ru.nosport.matrixaria.core.CoreVars;
import ru.nosport.matrixaria.core.MatrixariaXMLFormater;


import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.LogManager;
import java.util.logging.FileHandler;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;
import java.io.IOException;
import java.io.File;

import org.apache.commons.configuration.Configuration;

/**
 * User: vfabr
 * Date: 23.11.2006
 * Time: 14:13:44
 */
public class MBaseLogger implements BaseLogger, AppServerProcess {
	private LogManager lman = LogManager.getLogManager();
	private String path;
	//public final String instanceName;

	/**
	 *
	 */
	public MBaseLogger() {
		//this.instanceName = "MBaseLogger";
	}

	/**
	 *
	 * @param _conf
	 */
	public MBaseLogger(Configuration _conf) {
		this.path = _conf.getString("logDir");
		//this.instanceName = _conf.getString(CoreVars.getInstance().moduleInstanceName);
	}

	/**
	 *
	 * @param _obj
	 * @param _level
	 * @param _mess
	 */
	public void log(Object _obj, Level _level, String _mess) {
		//TODO: Log
	}

	/**
	 *
	 * @param _list
	 * @param _path
	 * @throws CoreException
	 */
	void setLogs(List<Package> _list, String _path) throws CoreException {
		CoreVars cv = CoreVars.getInstance();

		File logDir = new File(_path);
		if(!logDir.exists()){
			Boolean success = logDir.mkdirs();
			if(success){
				MLogger.get(this).info("Log path "+_path+" not found! Directory created.");
			}else{
				MLogger.get(this).warning("Log path "+_path+" not found! Directory is not created!");
			}
		}

		for (Package module : _list) {
			String logName = _path + cv.fileSysSep + module.getName() + ".log";
			try {
				Logger log = Logger.getLogger(module.getName());
				log.setLevel(Level.ALL);
				if(log.getHandlers().length == 0) {
					FileHandler logHandler = new FileHandler(logName, 0, 1, true);
					logHandler.setFormatter(new MatrixariaXMLFormater());
					logHandler.setLevel(Level.ALL);
					log.addHandler(logHandler);
				}
				lman.addLogger(log);
			} catch (IOException _ex) {
				MLogger.get(this).log(Level.SEVERE, "Can not create '"+ logName +"' user log. " + _ex.getMessage());
				throw new CoreException("Can not create '"+ logName +"' user log. " + _ex.getMessage());
			}
		}
	}

	/**
	 *
	 * @param _obj
	 * @return -
	 */
	public Logger get(Object _obj) {
		Logger tmpLog = this.lman.getLogger(_obj.getClass().getName());

		if (tmpLog == null) {
			tmpLog = Logger.getLogger(_obj.getClass().getName());
		}
		return tmpLog;
	}

	/**
	 *
	 * @param _list
	 * @return
	 * @throws CoreException
	 */
	private List<Package> distinctLogs(List<Package> _list) throws CoreException {
		Set<Package> tmp =  new HashSet<Package>(_list);
		CoreVars cv = CoreVars.getInstance();

		tmp.remove(Package.getPackage(cv.coreModuleLogName));
		tmp.remove(Package.getPackage(cv.userModuleLogName));
		tmp.remove(Package.getPackage(cv.remoteModuleLogName));
		tmp.remove(Package.getPackage(cv.coreLogName));

		return new ArrayList<Package>(tmp);
	}

	/**
	 *
	 * @return
	 * @throws CoreException
	 */
	public boolean moduleStart() throws CoreException {
		setLogs(this.distinctLogs(MLogger.getModules()), this.path);
		return false;
	}

	/**
	 *
	 * @return -
	 */
	public boolean moduleStop() {
		return false;
	}

	/**
	 *
	 * @return -
	 */
	public boolean isModuleRunning() {
		return false;
	}

	/**
	 *
	 * @return -
	 */
	public String moduleStat() {
		return null;
	}
}
